package com.yy.bolg.controller.evaluate;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yy.bolg.constant.ResultCode;
import com.yy.bolg.dao.FeedbackDao;
import com.yy.bolg.dao.WorkOrderDao;
import com.yy.bolg.entity.EvaluateBean;
import com.yy.bolg.entity.FeedbackBean;
import com.yy.bolg.exception.LackParameterException;
import com.yy.bolg.request.EvaluateRequest;
import com.yy.bolg.request.FeedbackRequest;
import com.yy.bolg.resp.EvaluateResp;
import com.yy.bolg.resp.FeedbackResp;
import com.yy.bolg.service.EvaluateService;
import com.yy.bolg.service.FeedbackService;
import com.yy.bolg.util.CodeGenerateUtil;
import com.yy.bolg.util.RespenseJsonTransformationUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 评价控制器
 * @author enmaoFu
 * @date 2024-12-13
 * RequestMapping("/evaluate") 是说明这个类的访问地址是 /evaluate
 */
@Api(tags = {"评价"})
@Controller
@RequestMapping(value = "/evaluate")
public class EvaluateController {

    @Autowired
    private EvaluateService service;// 自动载入Service对象

    @Autowired
    private WorkOrderDao workOrderDao;

    @Autowired
    private FeedbackDao feedbackDao;

    /**
     * 评价
     * 服务器访问路径：http://localhost:8080/bolg/evaluate/add
     * 本地访问路径：http://localhost:8111/evaluate/add
     * 业主评价参数：
     * {
     *     "feedbackCode": "FEED20241230230637594052",
     *     "workOrderCode": "WROR20241230230650",
     *     "evaluateCustomerCode": "CUS20241224151636",
     *     "evaluateCustomerUsername": "万里",
     *     "evaluateCustomerName": "人和街小学",
     *     "evaluateAdminCode": "",
     *     "evaluateAdminUsername": "",
     *     "electricianCode": "ELE20241224163321",
     *     "electricianName": "申跃",
     *     "evaluateCustomerScore": 4,
     *     "evaluateCustomerContent": "业主端评价",
     *     "evaluateAdminScore": 0,
     *     "evaluateAdminContent": ""
     * }
     * 总工评价参数：
     * {
     *     "feedbackCode": "FEED20241230230637594052",
     *     "workOrderCode": "WROR20241230230650",
     *     "evaluateCustomerCode": "",
     *     "evaluateCustomerUsername": "",
     *     "evaluateCustomerName": "",
     *     "evaluateAdminCode": "USER20241031211423",
     *     "evaluateAdminUsername": "刘德华",
     *     "electricianCode": "ELE20241224163321",
     *     "electricianName": "申跃",
     *     "evaluateCustomerScore": 0,
     *     "evaluateCustomerContent": "",
     *     "evaluateAdminScore": 3,
     *     "evaluateAdminContent": "总工评价"
     * }
     */
    @ApiOperation(value = "评价", notes = "参数：联系后端开发")
    @RequestMapping(value = "/add",method = RequestMethod.POST, produces="text/html;charset=UTF-8")
    @ResponseBody
    public String add(@RequestBody EvaluateRequest request){
        String result;

        EvaluateBean findOneByCodeEvaluateBean = new EvaluateBean();
        findOneByCodeEvaluateBean.setPsWorkOrderCode(request.getWorkOrderCode());

        try {
            EvaluateBean returnBean = service.findOneByCode(findOneByCodeEvaluateBean);

            if(null == feedbackDao.findOneByCode(request.getFeedbackCode())){
                result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, "没有该上报故障或预警可评价", new HashMap());
            }else if(null == workOrderDao.findOneByCode(request.getWorkOrderCode())){
                result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, "没有该工单可评价", new HashMap());
            }else if(null == returnBean){
                EvaluateBean evaluateBean = new EvaluateBean();

                if(!request.getEvaluateCustomerCode().equals("")){
                    evaluateBean.setPsEvaluateCustomerCode(request.getEvaluateCustomerCode());
                    evaluateBean.setPsEvaluateCustomerUsername(request.getEvaluateCustomerUsername());
                    evaluateBean.setPsEvaluateCustomerName(request.getEvaluateCustomerName());
                    evaluateBean.setPsEvaluateCustomerScore(request.getEvaluateCustomerScore());
                    evaluateBean.setPsEvaluateCustomerContent(request.getEvaluateCustomerContent());
                }else{
                    evaluateBean.setPsEvaluateAdminCode(request.getEvaluateAdminCode());
                    evaluateBean.setPsEvaluateAdminUsername(request.getEvaluateAdminUsername());
                    evaluateBean.setPsEvaluateAdminScore(request.getEvaluateAdminScore());
                    evaluateBean.setPsEvaluateAdminContent(request.getEvaluateAdminContent());
                }
                evaluateBean.setPsElectricianCode(request.getElectricianCode());
                evaluateBean.setPsElectricianName(request.getElectricianName());
                evaluateBean.setPsFeedbackCode(request.getFeedbackCode());
                evaluateBean.setPsWorkOrderCode(request.getWorkOrderCode());

                service.add(evaluateBean);

                result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "评价成功", new HashMap());
            }else {
                EvaluateBean evaluateBean = new EvaluateBean();
                if(null == returnBean.getPsEvaluateCustomerCode() || returnBean.getPsEvaluateCustomerCode().equals("")){
                    evaluateBean.setPsEvaluateCustomerCode(request.getEvaluateCustomerCode());
                    evaluateBean.setPsEvaluateCustomerUsername(request.getEvaluateCustomerUsername());
                    evaluateBean.setPsEvaluateCustomerName(request.getEvaluateCustomerName());
                    evaluateBean.setPsEvaluateCustomerScore(request.getEvaluateCustomerScore());
                    evaluateBean.setPsEvaluateCustomerContent(request.getEvaluateCustomerContent());

                    evaluateBean.setPsEvaluateAdminCode(returnBean.getPsEvaluateAdminCode());
                    evaluateBean.setPsEvaluateAdminUsername(returnBean.getPsEvaluateAdminUsername());
                    evaluateBean.setPsEvaluateAdminScore(returnBean.getPsEvaluateAdminScore());
                    evaluateBean.setPsEvaluateAdminContent(returnBean.getPsEvaluateAdminContent());
                }else{
                    evaluateBean.setPsEvaluateCustomerCode(returnBean.getPsEvaluateCustomerCode());
                    evaluateBean.setPsEvaluateCustomerUsername(returnBean.getPsEvaluateCustomerUsername());
                    evaluateBean.setPsEvaluateCustomerName(returnBean.getPsEvaluateCustomerName());
                    evaluateBean.setPsEvaluateCustomerScore(returnBean.getPsEvaluateCustomerScore());
                    evaluateBean.setPsEvaluateCustomerContent(returnBean.getPsEvaluateCustomerContent());

                    evaluateBean.setPsEvaluateAdminCode(request.getEvaluateAdminCode());
                    evaluateBean.setPsEvaluateAdminUsername(request.getEvaluateAdminUsername());
                    evaluateBean.setPsEvaluateAdminScore(request.getEvaluateAdminScore());
                    evaluateBean.setPsEvaluateAdminContent(request.getEvaluateAdminContent());
                }
                evaluateBean.setPsElectricianCode(request.getElectricianCode());
                evaluateBean.setPsElectricianName(request.getElectricianName());
                evaluateBean.setPsFeedbackCode(request.getFeedbackCode());
                evaluateBean.setPsWorkOrderCode(request.getWorkOrderCode());

                service.update(evaluateBean);

                result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "评价成功", new HashMap());
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, e.getMessage(),new HashMap());
        }
        return result;
    }

    /**
     * 根据电工编号查询所有评价
     * 服务器访问路径：http://localhost:8080/bolg/evaluate/findAllByElectricianCode
     * 本地访问路径：http://localhost:8111/evaluate/findAllByElectricianCode
     * 参数：
     * {"electricianCode":"ELE20241224163321"}
     */
    @ApiOperation(value = "根据电工编号查询所有评价", notes = "参数：{\"electricianCode\":\"ELE20241224163321\"}", response = EvaluateResp.class)
    @RequestMapping(value = "/findAllByElectricianCode",method = RequestMethod.POST,produces="text/html;charset=UTF-8")
    @ResponseBody
    public String findAllByElectricianCode(@RequestBody EvaluateRequest request){
        String result;

        EvaluateBean evaluateBean = new EvaluateBean();
        evaluateBean.setPsElectricianCode(request.getElectricianCode());

        try {
            List<EvaluateResp> respList = new ArrayList<>();
            for(EvaluateBean returnBean: service.findAllByElectricianCode(evaluateBean)){
                EvaluateResp evr = new EvaluateResp();
                evr.setFeedbackCode(returnBean.getPsFeedbackCode());
                evr.setWorkOrderCode(returnBean.getPsWorkOrderCode());
                evr.setEvaluateCustomerCode(returnBean.getPsEvaluateCustomerCode());
                evr.setEvaluateCustomerUsername(returnBean.getPsEvaluateCustomerUsername());
                evr.setEvaluateCustomerName(returnBean.getPsEvaluateCustomerName());
                evr.setEvaluateAdminCode(returnBean.getPsEvaluateAdminCode());
                evr.setEvaluateAdminUsername(returnBean.getPsEvaluateAdminUsername());
                evr.setElectricianCode(returnBean.getPsElectricianCode());
                evr.setElectricianName(returnBean.getPsElectricianName());
                evr.setEvaluateCustomerScore(returnBean.getPsEvaluateCustomerScore());
                evr.setEvaluateCustomerContent(returnBean.getPsEvaluateCustomerContent());
                evr.setEvaluateAdminScore(returnBean.getPsEvaluateAdminScore());
                evr.setEvaluateAdminContent(returnBean.getPsEvaluateAdminContent());
                evr.setCreateDate(returnBean.getCreateDate());
                respList.add(evr);
            }

            Map map = new HashMap();
            map.put("data", respList);
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "查询成功", map);
        } catch (Exception e) {
            e.printStackTrace();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, e.getMessage(),new HashMap());
        }
        return result;
    }

    /**
     * 根据业主编号查询所有评价
     * 服务器访问路径：http://localhost:8080/bolg/evaluate/findAllByEvaluateCustomerCode
     * 本地访问路径：http://localhost:8111/evaluate/findAllByEvaluateCustomerCode
     * 参数：{"evaluateCustomerCode":"CUS20241224151637"}
     */
    @ApiOperation(value = "根据电工编号查询所有评价", notes = "参数：{\"evaluateCustomerCode\":\"CUS20241224151637\"}", response = EvaluateResp.class)
    @RequestMapping(value = "/findAllByEvaluateCustomerCode",method = RequestMethod.POST,produces="text/html;charset=UTF-8")
    @ResponseBody
    public String findAllByEvaluateCustomerCode(@RequestBody EvaluateRequest request){
        String result;

        EvaluateBean evaluateBean = new EvaluateBean();
        evaluateBean.setPsEvaluateCustomerCode(request.getEvaluateCustomerCode());

        try {
            List<EvaluateResp> respList = new ArrayList<>();
            for(EvaluateBean returnBean: service.findAllByEvaluateCustomerCode(evaluateBean)){
                EvaluateResp evr = new EvaluateResp();
                evr.setFeedbackCode(returnBean.getPsFeedbackCode());
                evr.setWorkOrderCode(returnBean.getPsWorkOrderCode());
                evr.setEvaluateCustomerCode(returnBean.getPsEvaluateCustomerCode());
                evr.setEvaluateCustomerUsername(returnBean.getPsEvaluateCustomerUsername());
                evr.setEvaluateCustomerName(returnBean.getPsEvaluateCustomerName());
                evr.setEvaluateAdminCode(returnBean.getPsEvaluateAdminCode());
                evr.setEvaluateAdminUsername(returnBean.getPsEvaluateAdminUsername());
                evr.setElectricianCode(returnBean.getPsElectricianCode());
                evr.setElectricianName(returnBean.getPsElectricianName());
                evr.setEvaluateCustomerScore(returnBean.getPsEvaluateCustomerScore());
                evr.setEvaluateCustomerContent(returnBean.getPsEvaluateCustomerContent());
                evr.setEvaluateAdminScore(returnBean.getPsEvaluateAdminScore());
                evr.setEvaluateAdminContent(returnBean.getPsEvaluateAdminContent());
                evr.setCreateDate(returnBean.getCreateDate());
                respList.add(evr);
            }

            Map map = new HashMap();
            map.put("data", respList);
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "查询成功", map);
        } catch (Exception e) {
            e.printStackTrace();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, e.getMessage(),new HashMap());
        }
        return result;
    }

}
